
import 'dart:math';

import 'package:example/demo/page_ball.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_app/base/base_page.dart';
import 'package:flutter_base_app/base/base_page_view_model.dart';
import 'package:scrollview_observer/scrollview_observer.dart';

class PageListView extends StatefulWidget {
  const PageListView({super.key});

  @override
  State<PageListView> createState() => _PageOneState();
}

class _PageOneState extends BasePageState<BasePageViewModel, PageListView> with TickerProviderStateMixin {

  @override
  BasePageViewModel createViewModel() {
    return BasePageViewModel();
  }
  ScrollController scrollController = ScrollController();
  late ListObserverController observerController;
  @override
  void initState() {
    super.initState();
    for (var i = 150; i > 0; i--){
      var randomInt = Random().nextInt(20);
      if(randomInt <= 2){
        randomInt = 2;
      }
      dataList.add(ItemData(height: 50.0 * randomInt, test: "${dataList.length}"));
    }
    dataList[4].height = 1000;

    observerController =
        ListObserverController(controller: scrollController);
  }

  final List<ItemData> dataList = [];

  @override
  Widget buildContent(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        children: [
          SizedBox(height: 100,),
          Row(
            children: [
              GestureDetector(
                onTap: (){
                  setState(() {
                    dataList.insert(0, ItemData(height: 100, test: "${dataList.length}"));
                  });
                },
                child: Container(
                  padding: EdgeInsets.all(10),
                  color: Colors.red,
                  child: Text("add"),
                ),
              ),

              GestureDetector(
                onTap: (){
                  setState(() {
                    dataList.removeAt(0);
                  });
                },
                child: Container(
                  padding: EdgeInsets.all(10),
                  color: Colors.yellow,
                  child: Text("delete"),
                ),
              ),

              GestureDetector(
                onTap: (){
                  setState(() {
                    dataList[11].height = 100;
                  });
                },
                child: Container(
                  padding: EdgeInsets.all(10),
                  color: Colors.red,
                  child: Text("setheight"),
                ),
              ),

              GestureDetector(
                onTap: (){
                  observerController.jumpTo(index: 87);
                },
                child: Container(
                  padding: EdgeInsets.all(10),
                  color: Colors.yellow,
                  child: Text("jump"),
                ),
              ),

              GestureDetector(
                onTap: (){
                  scrollController.jumpTo(50000);
                },
                child: Container(
                  padding: EdgeInsets.all(10),
                  color: Colors.yellow,
                  child: Text("jump2"),
                ),
              ),
            ],
          ),
          Expanded(child: ListViewObserver(
            controller: observerController,
            child: ListView.builder(
              controller: scrollController,
              padding: EdgeInsets.zero,
              itemCount: dataList.length,
              itemBuilder: (BuildContext context, int index) {
                ItemData item = dataList[index];
                print("ziq build $index");
                return AnimatedContainer(
                  height: item.height,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Colors.green
                  ),
                  duration: Duration(milliseconds: 500),
                  child: Center(
                    child: Text(item.test),
                  ),
                );
              },),
          ),
          ),
          Container(height: 100, color: Colors.deepPurpleAccent,),
        ],
      ),
    );
  }



}

class ItemData {
  double height;
  String test;
  ItemData({required this.height, required this.test});
}
